/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;


import CRUD.ThreadHome;
import ModeratorOperations.ModeratorManageThreads;
import beans.EditActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;
import xmlMapping.Forum;
import xmlMapping.Thread;
/**
 *
 * @author Mostafa
 */
public class EditThreadAction extends DispatchAction {


     public ActionForward prepareEdit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
  
       Integer id=Integer.parseInt(request.getParameter("threadid"));
       Thread editthread=  new ThreadHome().findById(id);
      EditActionForm bean=new EditActionForm();
      bean.setBody(editthread.getBody());
      bean.setTitle(editthread.getTitle());
      bean.setThreadDescription(editthread.getThread_description());
      request.setAttribute("EditActionForm", bean);
        return mapping.findForward("editpage");
    }

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction2,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
 
 public ActionForward Edit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        EditActionForm mybean=(EditActionForm)form;
        Integer id=Integer.parseInt(request.getParameter("threadid"));
        ThreadHome th=new ThreadHome();
        Thread editthread= th.findById(id);
        String fname=editthread.getForum().getName();
        Integer fid=editthread.getForum().getForumId();
        editthread.setBody(mybean.getBody());
        editthread.setTitle(mybean.getTitle());
        editthread.setThread_description(mybean.getThreadDescription());
        th.session.close();
        ModeratorManageThreads moderatoroperation=new ModeratorManageThreads();
        moderatoroperation.editThread(editthread);
        
        
          response.sendRedirect("ViewForum.jsp?forumid="+fid+"&forumname="+fname);
        return null;
    }
    
    
    
    
    
}
